package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.PersonDao;
import com.njtech.pet.dao.PetDao;
import com.njtech.pet.entity.Person;
import com.njtech.pet.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Scanner;

/**
 * @author lxj
 * Date 2020-03-13 18:16
 * Description PersonDao test
 */
public class PersonDaoExample {
    static PersonDao personDao;
    static Scanner scanner;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        personDao = (PersonDao) context.getBean("personDao");

        System.out.println("准备开始测试" + personDao.toString());

        scanner = new Scanner(System.in);

        //start flag -1 end flag 0
        int num = -1;
        System.out.println("please enter test index:");
        System.out.println("0: exit,");
        System.out.println("1: test insert,");
        System.out.println("2: test update,");
        System.out.println("3: test select.");
        while (num != 0) {
            num = scanner.nextInt();
            switch (num) {
                case 0:
                    System.out.println("bye.");break;
                case 1:
                    insert();
                    break;
                case 2:
                    update();
                    break;
                case 3:
                    select();
                    break;
                default:
                    System.err.println("undefined index, please enter again.");
            }
            System.out.println("waiting for your command: ");
        }

    }

    static void insert() {
        System.out.println("user id:");
        int id = scanner.nextInt();
        Person person = new Person(null, "新建测试人员", "身份证", "testIdendity", "testphone", "testEmail", null, Constant.Person.STATE_VALID.getValue(), null, "父母", "");
        User user = new User();
        user.setId(id);
        person.setUser(user);
        personDao.insert(person);
        System.out.println("insert finished.");

    }

    private static void select() {
        System.out.println("user id:");
        int id = scanner.nextInt();
        User user = new User();
        List<Person> people = personDao.selectByUserId(user);
        System.out.println("selecting.");
        for (Person person :
                people) {
            System.out.println(person);
        }
    }

    private static void update() {
        System.out.println("person id : ");
        int id = scanner.nextInt();
        Person person = new Person(id, "修改的人员", "身份证", "updateIdendity", "updatephone", "updateEmail", null, Constant.Person.STATE_INVALID.getValue(), null, null, null);
        System.out.println("updating...");
        personDao.updateByPersonId(person);
    }
}
